package com.sunny.blog.common.config;

import com.sunny.blog.common.entity.SysConstant;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName: RabbitMQConfig
 * @Description: RabbitMQ配置
 * <li>1.创建死信交换机<li/>
 * <li>2.</li>
 * @Author: sunt
 * @Date: 2020/5/22 14:32
 * @Version 1.0
 **/
@Configuration
public class RabbitMQConfig {
    /**
     * 死信交换机
     * @return
     */
    @Bean
    public DirectExchange sysLogDelayExchange() {
        return new DirectExchange(SysConstant.SYS_LOG_DELAY_EXCHANGE);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue sysLogDelayQueue() {
        Map<String, Object> map = new HashMap<>(16);
        map.put("x-dead-letter-exchange",SysConstant.SYS_LOG_RECEIVE_EXCHANGE);
        map.put("x-dead-letter-routing-key", SysConstant.SYS_LOG_RECEIVE_KEY);
        return new Queue(SysConstant.SYS_LOG_DELAY_QUEUE, true, false, false, map);
    }

    /**
     * 给死信队列绑定交换机
     * @return
     */
    @Bean
    public Binding sysLogDelayBinding() {
        return BindingBuilder.bind(sysLogDelayQueue()).to(sysLogDelayExchange()).with(SysConstant.SYS_LOG_DELAY_KEY);
    }

    /**
     * 死信接收交换机,用于接收死信队列的消息
     * @return
     */
    @Bean
    public DirectExchange sysLogReceiveExchange() {
        return new DirectExchange(SysConstant.SYS_LOG_RECEIVE_EXCHANGE);
    }

    /**
     * 死信接收队列
     * @return
     */
    @Bean
    public Queue sysLogReceiveQueue() {
        return new Queue(SysConstant.SYS_LOG_RECEIVE_QUEUE);
    }

    /**
     * 死信交换机绑定消费队列
     * @return
     */
    @Bean
    public Binding sysLogReceiveBinding() {
        return BindingBuilder.bind(sysLogReceiveQueue()).to(sysLogReceiveExchange()).with(SysConstant.SYS_LOG_RECEIVE_KEY);
    }

}

